package com.bndg.hookalook;

import android.util.Log
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage


class MainHook : IXposedHookLoadPackage {
    override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
        // 过滤不必要的应用
        if (lpparam.packageName != "alook.browser") return
        XposedBridge.log("程序开始执行") // 打日志，说明模块开始执行 hook 操作
        // 执行Hook
        hook(lpparam)
    }

    private fun hook(lpparam: XC_LoadPackage.LoadPackageParam) {
        // 具体流程
        try {
            // Hook 内部类：u$a
            val adapterClass = "alook.browser.search.u\$a"
            XposedBridge.log("尝试加载类：$adapterClass")
            val clazz = lpparam.classLoader.loadClass(adapterClass)
                ?: run {
                    XposedBridge.log("没有找到类：$adapterClass")
                    return
                }
            XposedBridge.log("成功加载类：$adapterClass")
            // Hook v(Landroidx/recyclerview/widget/RecyclerView$ViewHolder;I)V
            XposedHelpers.findAndHookMethod(
                clazz,
                "v",
                "androidx.recyclerview.widget.RecyclerView\$w",
                Int::class.java,
                object : XC_MethodHook() {
                    override fun beforeHookedMethod(param: MethodHookParam?) {
                        param ?: return

                        val position = param.args[1] as Int
                        if (position != 0) return

                        val adapter = param.thisObject

                        // 查找类型为 alook.browser.search.u 的字段
                        val uClass = "alook.browser.search.u"
                        val f408d = adapter.javaClass.declaredFields
                            .find { field ->
                                field.type.name == uClass
                            }?.let {
                                it.isAccessible = true
                                it.get(adapter)
                            }

                        if (f408d == null) {
                            XposedBridge.log("找不到字段 f408d (类型: alook.browser.search.u)")
                            return
                        }

                        // 获取 d1 字段
                        val d1Field = f408d.javaClass.getDeclaredField("d1").apply {
                            isAccessible = true
                        }

                        val d1 = d1Field.getBoolean(f408d)
                        if (!d1) {
                            val viewHolder = param.args[0]

                            // 调用 P() 方法获取容器 ViewGroup
                            val pContainer = XposedHelpers.callMethod(viewHolder, "P") as? ViewGroup
                            pContainer?.removeAllViews()

                            // 阻止原方法执行
                            param.result = null
                        }
                    }
                }
            )
        } catch (e: Throwable) {
            XposedBridge.log(e)
        }
    }
}